﻿Imports System.Data.SqlClient

Public Class DoiTacDAO
    Public Shared Function Insert(ByVal dt As DoiTacDTO) As Integer
        Dim str = String.Format("Insert into DoiTac(MaDoiTac,TenDoiTac,DiaChi,MST,KiHieuHoaDon,DienThoai,NguoiLienLac,Email) values ('{0}',N'{1}',N'{2}',N'{3}',N'{4}',N'{5}',N'{6}',N'{7}')", dt.MaDoiTac, dt.TenDoiTac, dt.DiaChi, dt.MST, dt.KiHieuHoaDon, dt.DienThoai, dt.NguoiLienLac, dt.Email)
        Dim connect As SqlConnection = DataProvider.ConnectDB("QuanLyHangHoa")
        connect.Open()
        Dim cmd As New SqlCommand(str, connect)
        cmd.CommandType = CommandType.Text
        Dim result As Integer = 1
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            result = 0
        End Try

        Return result
    End Function

    Public Shared Function Delete(ByVal maDT As String) As Integer
        Dim result As Integer = 1
        Dim StrSql As String
        StrSql = "Delete from DOITAC where MaDoiTac = '" & maDT & "'"
        Try
            SQLProvider.ExecNoneQuery(StrSql)
        Catch ex As Exception
            result = 0
        End Try
        Return result
    End Function

    Public Shared Function Update(ByVal dt As DoiTacDTO) As Integer
        Dim result As Integer = 1
        Dim StrSql As String
        StrSql = "Update DOITAC set TenDoiTac = N'" & dt.TenDoiTac & "',DiaChi = N'" & dt.DiaChi & "',MST = '" & dt.MST & "',KiHieuHoaDon = '" & dt.KiHieuHoaDon & "',DienThoai = '" & dt.DienThoai & "',NguoiLienLac = N'" & dt.NguoiLienLac & "',Email = '" & dt.Email & "'" & " Where MaDoiTac = '" & dt.MaDoiTac & "'"
        Try
            SQLProvider.ExecNoneQuery(StrSql)
        Catch ex As Exception
            result = 0
        End Try
        Return result
    End Function

    Public Shared Function Select_All() As DataTable
        Dim dt As DataTable
        Dim StrSql As String
        StrSql = "Select * from DOITAC"
        dt = SQLProvider.ExecQuery(StrSql)
        Return dt
    End Function

    Public Shared Function Select_By_Ma(ByVal maDT As String) As DataTable
        Dim dt As DataTable
        Dim StrSql As String
        StrSql = "Select * from DOITAC where MaDoiTac = '" & maDT & "'"
        dt = SQLProvider.ExecQuery(StrSql)
        Return dt
    End Function

    Public Shared Function Search(ByVal mDT As String) As DataTable
        Dim dt As New DataTable
        Dim StrSql As String
        StrSql = "Select * from DOITAC Where MaDoiTac like '%" & mDT & "%'"
        dt = SQLProvider.ExecQuery(StrSql)
        Return dt
    End Function

End Class
